package cmd

import (
	"fmt"
	"os"
	"vblog/conf"
	"vblog/ioc"

	"github.com/spf13/cobra"
)

var (
	configPath string

	RootCmd = &cobra.Command{
		Use:   "vblog",
		Short: "A generator for Cobra based Applications",
		Long:  ``,
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) > 0 {
				if args[0] == "verson" {
					fmt.Println("v0.0.1")
				}
			} else {
				cmd.Help()
			}

		},
	}
)

func Execute() error {
	cobra.OnInitialize(
		func() {
			configPath = os.Getenv("CONFIG_PATH")

			// fmt.Printf("cobra.OnInitialize %s\n", configPath)

			if configPath == "" {
				configPath = "etc/application.yml"
			}

			cobra.CheckErr(conf.LoadConfigFromYaml(configPath))
			cobra.CheckErr(ioc.Controller.Init())
			cobra.CheckErr(ioc.Api.Init())
		})
	return RootCmd.Execute()
}
func init() {
	RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "etc/application.yml", "config file")
	RootCmd.AddCommand(StartCmd)
}
